//
//  TaskListController.m
//  Safflower
//
//  Created by 袁亮 on 2016/12/21.
//  Copyright © 2016年 Migic_Z. All rights reserved.
//

#import "TaskListController.h"
#import "TaskListView.h"
#import "TaskDataCenter.h"
#import "ReceiveTaskController.h"
#import "SignMainController.h"
#import "MineTaskListController.h"

@interface TaskListController ()<TaskListViewDelegate, ReceiveTaskControllerDelegate>

@property (nonatomic, strong) TaskListView *listView;
@property (nonatomic, strong) TaskDataCenter *dataCenter;

@end

@implementation TaskListController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.baseTitle = @"任务列表";
    self.rightBtnTitle = @"返回";
    
    _dataCenter = [TaskDataCenter new];
    
    [self makeTaskListView];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
}

- (void)makeTaskListView
{
    _listView = [TaskListView new];
    _listView.delegate = self;
    [self.baseView addSubview:_listView];
    [_listView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.left.right.mas_equalTo(0);
    }];
}

- (void)headerRefresh:(NSInteger)page
{
    [self loadTaskListData:page];
}

- (void)footerLoadMore:(NSInteger)page
{
    [self loadTaskListData:page];
}

- (void)loadTaskListData:(NSInteger)page
{
    [_dataCenter fetchTaskListWithPage:page Complation:^(id data, NSString *error) {
        
        if (error == nil) {
            [_listView loadTaskListData:(NSArray *)data];
        }else{
            [YJProgressHud showErrorHud:error];
        }
        
    }];
}

- (void)didSelectTask:(TaskModel *)taskModel index:(NSInteger)index
{
    TaskModel *task = taskModel;
    
    if ([task.enddate integerValue] < [[ZZDateFormat fetchNowDateForUnix] integerValue]) {
        [YJProgressHud showErrorHud:@"任务已过期了哦~~~"];
    }else{
        if (task.isreceive == YES) {
            NSString *todayUnix = [ZZDateFormat fetchNowDateForUnix];
            NSString *dateString = [ZZDateFormat fetchUnixDate:[ZZDateFormat fetch10Date:todayUnix format:@"yyyyMMdd"] format:@"yyyyMMdd"];
            if ([dateString integerValue] < [task.startdate integerValue] ) {
                NSInteger startDay = [ZZDateFormat fetchNumberOfStartDate:todayUnix endDate:taskModel.startdate];
                [YJProgressHud showErrorHud:[NSString stringWithFormat:@"距离任务开始还有%lu天~~~",startDay]];
            }else{
                SignMainController *signController = [[SignMainController alloc] init];
                signController.taskModel = taskModel;
                [self.navigationController pushViewController:signController animated:YES];
            }
            
        }else{
            ReceiveTaskController *receiveTaskController = [[ReceiveTaskController alloc] init];
            receiveTaskController.taskModel = taskModel;
            receiveTaskController.delegate = self;
            [self.navigationController pushViewController:receiveTaskController animated:YES];
        }
    }
    
    
}

- (void)clickMineTaskList
{
    MineTaskListController *mineTaskListController = [[MineTaskListController alloc] init];
    BaseNavigationController *mineTaskNavigationController = [[BaseNavigationController alloc] initWithRootViewController:mineTaskListController];
    [self presentViewController:mineTaskNavigationController animated:NO completion:nil];
}

- (void)receiveSuccessReloadData
{
    [_listView headerRefreshListData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
